<template>
	<div class="bar_buy">
		<div class="left_block">
			<a :href="'/comment/edit?source_table=goods&source_field=goods_id&source_id=' + obj.goods_id" class="collect_box">
				<i class="fa-chat"></i>
				<span>评论</span>
			</a>
			<a @click="add_collect()" class="publish_box">
				<i class="fa-heart"></i>
				<span>收藏</span>
			</a>
		</div>
		<div class="right_block">
			<div class="addCart" @click="add_cart()">加入购物车</div>
			<div class="buy" @click="add_order()">立即购买</div>
		</div>
	</div>
</template>

<script>
	export default {
		props: {
			obj: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				// 是否已收藏
				check_collected: false
			}
		},
		methods: {
			// 收藏
			add_collect() {
				var {
					title,
					img,
					goods_id
				} = this.obj
				var body = {
					title,
					img,
					source_table: "goods",
					source_field: "goods_id",
					source_id: goods_id,
					user_id: this.$store.state.user.user_id
				}
				if (!this.check_collected) {
					this.check_collected = true
					this.$post('~/api/collect/add?', body, (res) => {
						this.$toast("收藏成功");
					})
				} else {
					this.check_collected = false
					this.$get('~/api/collect/del', body, (res) => {
						this.$toast("取消收藏");
					})
				}
			},
			// 添加购物车
			add_cart() {
				var {
					title,
					img,
					price,
					price_ago,
					description,
					goods_id
				} = this.obj
				var body = {
					title,
					img,
					price,
					price_ago,
					description,
					goods_id,
					user_id: this.$store.state.user.user_id
				}
				this.$post('~/api/cart/add?', body, (res) => {
					this.$toast("加入成功");
				})
			},
			// 立即购买,加入订单
			add_order() {
				// var {
				// 	title,
				// 	img,
				// 	price,
				// 	price_ago,
				// 	description,
				// 	goods_id
				// } = this.obj
				// var body = {
				// 	title,
				// 	img,
				// 	price,
				// 	price_ago,
				// 	description,
				// 	goods_id,
				// 	user_id: this.$store.state.user.user_id
				// }
				// this.$post('~/api/order/add?', body, (res) => {
				// 	this.$toast("加入订单");
				// 	console.log(res);
				// })
			}
			
		},
		computed: {}
	}
</script>

<style>
	.bar_buy {
		display: flex;
		background-color: var(--color_white);
		border: 1px solid #eee;

	}

	.bar_buy>div {
		height: 3rem;
		line-height: 3rem;
	}

	.left_block {
		flex: 1;
		display: flex;
		justify-content: space-evenly;
		;
	}

	.left_block>a {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.left_block text {
		color: var(--color_grey);
		font-size: 0.5rem;
		padding-left: 0.1rem;
	}

	.right_block {
		flex: 1.5;
		display: flex;
		color: #fff;
	}

	.right_block>div {
		flex: 1;
		text-align: center;
		vertical-align: center;
	}

	.addCart {
		background-color: var(--color_orange);
	}

	.buy {
		background-color: var(--color_red);
	}
</style>
